package com.example.shirodemo.controllers;

import com.example.core.libs.AppException;
import com.example.core.libs.JSONResult;
import com.example.core.utils.JwtUtils;
import com.example.core.utils.RedisUtil;
import com.example.shirodemo.authentication.ShiroHelper;
import com.example.shirodemo.entities.User;
import com.example.shirodemo.services.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

@RestController
@Slf4j
@Validated
public class LoginController extends BaseController {
    @Autowired
    IUserService userService;
    @Autowired
    JwtUtils jwtUtils;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    ShiroHelper shiroHelper;

    @RequestMapping("/login")
    public JSONResult login(@NotBlank(message = "账户不能为空") String username,
                            @NotBlank(message = "密码不能为空") String password
//                            boolean rememberMe
    ) {
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);

        User user = userService.findByName(username);
        if (user == null) {
            throw new AppException("用户不存在！");
        }
        log.info("user=" + user.toString());

        try {
            super.login(token);

            // 生成koen并存入reids
            String ntoken = jwtUtils.generateToken(String.valueOf(user.getId()));
            redisUtil.hmSet(RedisUtil.USER_REDIS_LOGIN, String.valueOf(user.getId()), ntoken);

            return new JSONResult().put("token", ntoken);
        } catch (UnknownAccountException | IncorrectCredentialsException | LockedAccountException e) {
            throw new AppException(e.getMessage());
        } catch (AuthenticationException e) {
            e.printStackTrace();
            throw new AppException("认证失败！");
        }
    }

    @RequestMapping("/index")
    public String index() {
        return "欢迎来到首页";
    }

    @RequestMapping("/list")
    @RequiresPermissions("user:view")
    public JSONResult list() {
        return new JSONResult().put("list", userService.list());
    }

    @RequestMapping("/add")
    @RequiresPermissions("user:add")
    public String add() {
        return "新增成功";
    }

    @RequestMapping("/unauthorized")
    public String unauthorized() {
        return "您还没有权限访问哦";
    }

    @RequestMapping("/logout")
    public String logout() {
        shiroHelper.logout();
        return "您已经退出登录";
    }

    @RequestMapping("/clearCache")
    public String clearCache() {
        shiroHelper.clearCache();
        return "缓存删除成功";
    }
}
